$(function(){
	$(document).ready(function () {
	   
		initSchemaTypeSelect();
		initCustSchemaTable();
		
		loadRequest();
		loadRespInfo();
		
		
		
	});	
	
	$(document).on("click","#close",function(){
		$("#rootPoint1").treegrid("clear");
		$("#addRoot1").hide();
		$("#table1").hide();
	});
	
	$(document).on("click","#add_response_btn",function(){
		$("#code").val("");
		$("#description").val("");
		$("#columnName").val("");
		$("#type").val("String");
		$("#rootPoint").treegrid("clear");
		$("#addRoot").hide();
		$("#rootPoint").hide();
	});
	/*
	 * 异步保存
	 */
	$(document).on("click","#saveResponse",function(){
		saveDetail();
		setTimeout(function(){$("#AddModal").modal("hide")},100);
	});
	
	$(document).on("click","#edit_btn",function(){
		var id = $(this).attr("value");
		var apiId = $("#apiId").attr("value");
		var tr = $("#tr_"+id);
		var code = tr.find("td:eq(1)").find("input").val();
		var description = tr.find("td:eq(2)").find("input").val();
		var location = tr.find("td:eq(3)").find("select").val();
		var type = tr.find("td:eq(4)").find("select").val();
		var isMust = tr.find("td:eq(5)").find("select").val();
		var conditionContent = tr.find("td:eq(6)").find("input").val();
		var restApiRequest = new RestApiRequest(id, code, description, location, type, isMust, conditionContent, apiId)
		updateDetail(restApiRequest);
	});
	
	/*
	 * response
	 */
	$(document).on("click","#edit_response_btn",function(){
		var responseId = $(this).attr("value");
		var apiId = $("#apiId").attr("value");
		updatePre(responseId);
	});
	
	/*
	 * 更新时保存
	 */
	$(document).on("click","#updateResponse",function(){
		updateResponse();
	});
	
	/*保存自定义sql*/
	$(document).on("click","#saveSql",function(){
		sqlDesign();
		setTimeout(function(){$("#SqlModal").modal("hide")},100);
	});
})

function initSchemaTypeSelect(){
	$("#type").change(function(){
		var type = $("#type").val();
		// 处理自定义结构
		if(type == "object" || type == "array"){
			$("#rootPoint").show();
			$("#rootPoint1").show();
			$("#addRoot").show();
			$("#addRoot1").show();
			
		}
		else{
			$("#rootPoint").hide();
			$("#rootPoint1").hide();
			$("#addRoot").hide();
			$("#addRoot1").hide();
		}
		
		
		
	});
}
/* 初始化自定义结构表格 */
function initCustSchemaTable(){
		$("#rootPoint").treegrid({
		onAdded:function(row){
			var $row = $(row);
			
			// 初始化右键菜单
			initRespContextMenu($row);
		
			// 初始化引用下拉框
			$row.find(".cust-schema-type").trigger("change");
			
		}
	});
		// 更新
		$("#rootPoint1").treegrid({
			onAdded:function(row){
				var $row = $(row);
			
				// 初始化右键菜单
				initRespContextMenu($row);
				
			
				// 初始化引用下拉框
				$row.find(".cust-schema-type").trigger("change");
				
			}
		});
	
	// 初始化表格右键菜单
	initRespContextMenu("#rootPoint tbody tr");
	initRespContextMenu("#rootPoint1 tbody tr");
	
	
	// 新增根节点
	$("#addRoot").click(function(){
		
		var nodeObje = getTmpl("#custSchemaTmpl");
		
	    $("#rootPoint").treegrid("addRootNode",nodeObje);
	});
	// 更新弹窗
    $("#addRoot1").click(function(){
		
		var nodeObje = getTmpl("#custSchemaTmpl");
		
	    $("#rootPoint1").treegrid("addRootNode",nodeObje);
	});
	
}
function getTmpl(selector){
	return $($(selector).html());
}


/* 初始化自定义结构表格右键菜单 */
function initRespContextMenu(selector){
	
	
	$(selector).contextPopup({
        items: [
                {
            	label:'新增',     
            	clazz:'fa fa-plus context-color',
            	
                 action:function(event,row) { 
                	 var custSchemaTypeSelect = $(row).find(".cust-schema-type");
             		var type = custSchemaTypeSelect.val();
             		if (type == "object" || type == "array") {
            		$(row).treegrid("addChildNode",getTmpl("#custSchemaTmpl"));
             		}
            	},
            
            	
            	
            },
            {
            	label:'删除',     
            	clazz:'fa fa-remove context-color',
            	action:function(event,row) { 
            		$(row).treegrid("removeNodes");
            	}
            }
        ]
    });
}


/*
 * 实体类
 */ 
function RestApiRequest(id,code,description,location,type,isMust,conditionContent,apiId){
	this.id = id;
	this.code = code;
	this.description = description;
	this.location = location;
	this.type = type;
	this.isMust = isMust;
	this.conditionContent = conditionContent;
	this.apiId = apiId;
}

function RestApiResponse(id,code,description,columnName,type,apiId){
	this.id = id;
	this.code = code;
	this.description = description;
	this.columnName = columnName;
	this.type = type;
	this.apiId = apiId;
}
/*
 * 更新request
 */
function updateDetail(restApiRequest){
	var apiId = $("#apiId").attr("value");
	var id = restApiRequest.id;
	var code = restApiRequest.code;
	var description = restApiRequest.description;
	var conditionContent = restApiRequest.conditionContent;
	var location = restApiRequest.location;
	var isMust = restApiRequest.isMust;
	var type = restApiRequest.type;
	$.ajax({
		type: 'POST',
		url: ctx + '/api/'+ apiId +'/request/update',
		dataType: 'json',
		data: {id:id,
			   code:code,
			   description:description,
			   conditionContent:conditionContent,
			   location:location,
			   type:type,
			   isMust:isMust
			   },
		success:function(data){
			if(data.code ==200){
			}
		}
	});
}

function saveRequest(){
	var apiId = $("#apiId").val();
	var code = $("#requestCode").val();
	var description = $("#requestDescription").val();
	var location = $("#requestLocation").val();
	var type = $("#requestType").val();
	var isMust = $("#requestIsMust").val();
	var condition = $("#requestCondition").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/request/save',
		data: {apiId:apiId,code:code,description:description,location:location,type:type,isMust:isMust,condition:condition},
		success:function(data){
			if(data.code==200){
				deleteRow1();
				loadRequest();
			}
		}
	});
}

/*
 * 更新response
 */
function updateResponse(){
	var apiId = $("#apiId").attr("value");
	var responseId = $("#responseId").val();
	
	/*var code = $("#update_code").val();
	var description = $("#update_description").val();
	var columnName = $("#update_columnName").val();
	var type = $("#update_type").val();
*/
	var param = serializeCustSchemaForm1();
	/*param.apiId=apiId;
	param.responseId=responseId;
	param.type=type;
	
	param.code=code;
	param.description=description;*/
	
//	alert(JSON.stringify(param));
	if(code!=""){
	
	var columnName = $("#columnName").val();
	var type = $("#type").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/update',
		data:param,
		success:function(data){
			if(data.code == 200){
				setTimeout(function(){$("#updateModal").modal("hide")},100);
				loadRespInfo();
			}
		   }
		});
	}else{
		$("#code_text").text("编码不能为空");
	}
	
}


/*
 * 加载response数据
 */
function loadDetail(){
	var apiId = $("#apiId").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/list',
		data: {apiId:apiId},
		success:function(data){
		  if(data.code == 200){
			  var len = data.responseList.length;
			  var itemsHtml = '';
			  var columnName = '';
			  for(var i=0; i<len; i++){
	        		(function(){
	        			var res = data.responseList[i];
	        			var pro = data.project;
	        			if(pro.textOnly){
	        				
	        			}else{
	        				columnName = '<td><input type="text" class="form-control" id="columnName'+res.id+'" value="'+res.columnName+'" onblur="checkColumn(columnName'+res.id+');" placeholder="例如 y:x[1]" readonly="readonly"></td>';
	        			}
	        			itemsHtml += '<tr id="tr1_'+res.id+'">'+
	        							'<td>'+(i+1)+'</td>'+
	        							'<td><input type="text" class="form-control" id="code'+res.id+'" value="'+res.code+'" readonly="readonly"></td>'+
	        							'<td><input type="text" class="form-control" id="description'+res.id+'" value="'+res.description+'" readonly="readonly"></td>'+
	        							columnName +
	        							'<td><select class="form-control" id="select" disabled="disabled"><option>'+res.type+'</option></select></td>'+
	        							'<td><a id="edit_response_btn" class="btn btn-primary btn-sm" value="'+res.id+'" style="float: left;" href="javascript:void(0);" data-toggle="modal" data-target="#updateModal">编辑</a>'+
	    			   		  			'<a id="delete_response_btn" class="btn btn-danger btn-sm" value="'+res.id+'" style="float: right;" href="javascript:void(0);" onclick="deleteResponse('+res.id+');">删除</a></td>'+
	        						 '</tr>'
	        			})(i);
	        		}
			  $('#responseList').html(itemsHtml);
		  }
		}
	});
}

/*
 * 加载request数据
 */
function loadRequest(){
	var apiId = $("#apiId").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/request/list',
		data: {apiId:apiId},
		success:function(data){
			
			if(data.code==200){
				  var len = data.list.length;
				  var itemsHtml = '';
				  for(var i=0; i<len; i++){
		        		(function(){
		        			var res = data.list[i];
		        			if(res.location=='query')
		        			{
		        				var location = '<option>query</option>'+
								   '<option>path</option>'+
								   '<option>body</option>'+
								   '<option>formData</option>'+
								   '<option>header</option>'+
								   '<option>cookie</option>';
		        			}
		        			
		        			if(res.location=='path'){
		        				var location = '<option>query</option>'+
								   '<option selected="selected">path</option>'+
								   '<option>body</option>'+
								   '<option>formData</option>'+
								   '<option>header</option>'+
								   '<option>cookie</option>';
		        			}
			        				
		        			if(res.location=='body'){
			        			var location = '<option>query</option>'+
									   '<option>path</option>'+
									   '<option selected="selected">body</option>'+
									   '<option>formData</option>'+
									   '<option>header</option>'+
									   '<option>cookie</option>';
		        			}	
		        			
		        			if(res.location=='formData'){
		        				var location = '<option>query</option>'+
								   '<option>path</option>'+
								   '<option>body</option>'+
								   '<option selected="selected">formData</option>'+
								   '<option>header</option>'+
								   '<option>cookie</option>';
		        			}
		        				
		        			if(res.location=='header'){
		        				var location = '<option>query</option>'+
								   '<option>path</option>'+
								   '<option>body</option>'+
								   '<option>formData</option>'+
								   '<option selected="selected">header</option>'+
								   '<option>cookie</option>';
		        			}
		        				
		        			if(res.location=='cookie'){
		        				var location = '<option>query</option>'+
								   '<option>path</option>'+
								   '<option>body</option>'+
								   '<option>formData</option>'+
								   '<option>header</option>'+
								   '<option selected="selected">cookie</option>';
		        			}
		        				
		        			
		        			if(res.type=='String'){
		        				var type = '<option selected="selected">String</option>'+
								   '<option>int</option>'+
								   '<option>short</option>' +
						           '<option>byte</option>' +
								   '<option>boolean</option>'+
								   '<option>long</option>'+
								   '<option>double</option>'+
								   '<option>float</option>'+
								   '<option>decimal</option>'+
								   '<option>file</option>';
		        			}
		        			
		        			if(res.type=='int'){
			        				var type = '<option>String</option>'+
									   '<option selected="selected">int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option>long</option>'+
									   '<option>double</option>'+
									   '<option>float</option>'+
									   '<option>decimal</option>'+
									   '<option>file</option>';
		        			}
		        			
		        			if(res.type=='short'){
		        				var type = '<option>String</option>'+
								   '<option>int</option>'+
								   '<option selected="selected">short</option>' +
						           '<option>byte</option>' +
								   '<option>boolean</option>'+
								   '<option>long</option>'+
								   '<option>double</option>'+
								   '<option>float</option>'+
								   '<option>decimal</option>'+
								   '<option>file</option>';
		        			}
		        			
		        			if(res.type=='byte'){
		        				var type = '<option>String</option>'+
								   '<option>int</option>'+
								   '<option>short</option>' +
						           '<option selected="selected">byte</option>' +
								   '<option>boolean</option>'+
								   '<option>long</option>'+
								   '<option>double</option>'+
								   '<option>float</option>'+
								   '<option>decimal</option>'+
								   '<option>file</option>';
		        			}
		        			
		        			if(res.type=='boolean'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option selected="selected">boolean</option>'+
									   '<option>long</option>'+
									   '<option>double</option>'+
									   '<option>float</option>'+
									   '<option>decimal</option>'+
									   '<option>file</option>';
		        			}
		        			
		        			if(res.type=='long'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option selected="selected">long</option>'+
									   '<option>double</option>'+
									   '<option>float</option>'+
									   '<option>decimal</option>'+
									   '<option>file</option>';
		        			}
			        			
		        			if(res.type=='double'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option>long</option>'+
									   '<option selected="selected">double</option>'+
									   '<option>float</option>'+
									   '<option>decimal</option>'+
									   '<option>file</option>';
		        			}
			        			
		        			if(res.type=='float'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option>long</option>'+
									   '<option>double</option>'+
									   '<option selected="selected">float</option>'+
									   '<option>decimal</option>'+
									   '<option>file</option>';
		        			}
			        				
		        			if(res.type=='decimal'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option>long</option>'+
									   '<option>double</option>'+
									   '<option>float</option>'+
									   '<option selected="selected">decimal</option>'+
									   '<option>file</option>';
		        			}
			        				
		        			if(res.type=='file'){
			        				var type = '<option>String</option>'+
									   '<option>int</option>'+
									   '<option>short</option>' +
							           '<option>byte</option>' +
									   '<option>boolean</option>'+
									   '<option>long</option>'+
									   '<option>double</option>'+
									   '<option>float</option>'+
									   '<option>decimal</option>'+
									   '<option selected="selected">file</option>';
		        			}
		        			
		        			if(res.isMust=='true'){
		        				var isMust = '<option selected="selected">true</option>'+
								   '<option>false</option>';
		        			}
		        			
		        			if(res.isMust=='false'){
			        				var isMust = '<option>true</option>'+
									   '<option selected="selected">false</option>';
		        			}
		        			itemsHtml += '<tr id="tr_'+res.id+'">'+
		        							'<td>'+(i+1)+'</td>'+
		        							'<td><input type="text" class="form-control" id="code'+res.id+'" value="'+res.code+'"></td>'+
		        							'<td><input type="text" class="form-control" id="description'+res.id+'" value="'+res.description+'"></td>'+
		        							'<td><select class="form-control" id="location${request.id}">'+ location +'</select></td>'+
		        							'<td><select class="form-control" id="type${request.id}">'+ type +'</select></td>'+
		        							'<td><select class="form-control" id="isMust${request.id}">'+ isMust +'</select></td>'+
		        							/*
											 * '<td><input type="text"
											 * class="form-control"
											 * id="condition${request.id}"
											 * value="'+res.condition+'"></td>'+
											 */
		        							'<td><input type="hidden" id="id" value="${request.id}">'+
		        							'<a id="edit_btn" class="btn btn-primary btn-sm" value="'+ res.id +'" style="float: left;" href="javascript:void(0);" onclick="updateConfirm();">保存</a>'+
		        							'<a id="delete_btn" class="btn btn-danger btn-sm" value="'+ res.id +'" style="float: right;" href="javascript:void(0);" onclick="deleteReq('+res.id+');">删除</a>'+
		        							'</td>'+
		        						 '</tr>'
		        			})(i);
		        		}
				  $('#requestList').html(itemsHtml);
			}
		}
	});
}

/**
 * 保存response
 */
function saveDetail(){
	var apiId = $("#apiId").val();
	var code = $("#code").val();
	var description = $("#description").val();
	var param = serializeCustSchemaForm();
	param.code=code;
	param.description=description;
//	alert(JSON.stringify(param));
	if(code!=""){
	
	var columnName = $("#columnName").val();
	var type = $("#type").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/save',
		data:param,
		success:function(data){
				
				if(data.code == 200){

					setTimeout(function(){$("#AddModal").modal("hide")},100);
					
					loadRespInfo();
					
				}
				/*
				setTimeout(function(){$("#AddModal").modal("hide")},100);
				if(data.type!="object"||data.type!="array") {
				loadRespInfo();
				}
				var responseId = data.responseId;
		
				
				var nodeArray = new Array();
				$("#rootPoint").treegrid("getAllNodes").each(function(index,element){
					var param1 = new Object();
					var $element = $(element);
					var responseId = data.responseId;
					var rowData = $element.find("*").getFieldsValue();
					
			       
					rowData.nodeId = $element.treegrid("getNodeId");
					
					rowData.parentId = $element.treegrid("getParentNodeId");
					if(rowData.parentId==null) {
						
						rowData.parentId=0;
					}
				 	rowData.responseId = responseId;
				 	//alert(JSON.stringify(rowData));
				    
					$.ajax({
						type: 'POST',
						dataType: 'json',
						url: ctx + '/api/' + apiId + '/responseveRoot',
						data:rowData,
						success:function(data){
							if(data.code == 200){
								loadRespInfo();
								
							}
						}
					});
			});
					
				
			 */
		   }
		});
	}else{
		$("#code_text").text("编码不能为空");
	}
}


/*
 * 更新前查询response
 */
function updatePre(responseId){
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/updatePre',
		data: {responseId:responseId},
		success:function(data){
			
			if(data.code==200){
				var itemsHtml = '';
				var response = data.response;
				$("#responseId").val(response.id);
				var list = data.nodeList;
				var len = list.length;
				$("#update_code").val(response.code);
				$("#update_description").val(response.description);
				$("#update_columnName").val(response.columnName);
				$("#update_type").val(response.type);
				var rowList = JSON.stringify(list);
				$("#rootPoint1").treegrid("clear");
				
				var rowData;
				var nodeObj;
				var nodeObjCache = new Object();
				if(response.type=="array"||response.type=="object"){
					$("#table1").show();
					$("#addRoot1").show();
					
				for(var i = 0;i < len; i ++){
				
						nodeObj = getTmpl("#custSchemaTmpl");
					
						rowData = list[i];
						
					    if(rowData.parentId==null){
						
							$("#rootPoint1").treegrid("addRootNode",nodeObj,rowData.nodeId);
						}
						else{
							nodeObjCache[rowData.parentId].treegrid("addChildNode",nodeObj,rowData.nodeId,rowData.parentId);
						}
						
						nodeObj.find("*").setFieldsValue(rowData);
						// 填充值后初始化引用下拉框
						nodeObj.find(".cust-schema-type").trigger("change");
						
						nodeObjCache[rowData.nodeId] = nodeObj;
						
						
					}
				}
				
		     						
					 
			
		}
	}
	});
}



function deleteRow2(){
	 var _tab=document.getElementById("responseTab");
	 var index = _tab.rows.length;
	 var _row=_tab.deleteRow(index-1);
}
/*
 * 防止多次新增一行tr
 */
function checkRow(id){
	var trId = $("#"+id).find("tr").last().attr("id");
	var index = $("#"+id).find("tr").length;
	if(trId!=null||index==1){
		if(id=="requestTab"){
			addRow1(id);
		}
		if(id=="responseTab"){
			addRow2(id);
		}
	}else{
		confirm_info("请先完善当前信息！");
	}
}
/*
 * 删除response
 */
function deleteResponse(id){
	confirm_tip("确定删除？",function(flag){
		if(flag==1){
			 deleteDetail(id);
		}
	});
}
/*
 * 删除request
 */
function deleteReq(id){
	confirm_tip("确定删除？",function(flag){
		if(flag==1){
			 deleteRequest(id);
		}
	});
}
/*
 * 删除response
 */
function deleteDetail(id){
	var apiId = $("#apiId").val();
	var responseId = $("#responseId").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/delete',
		data: {id:id,responseId:responseId},
		success:function(data){
			if(data.code==200){
				// loadDetail();
				loadRespInfo();
			}
		}
	});
}
/*
 * 删除request
 */
function deleteRequest(id){
	var apiId = $("#apiId").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/request/delete',
		data: {id:id},
		success:function(data){
			if(data.code==200){
				loadRequest();
			}
		}
	});
}
/*
 * 更新提示
 */
function updateConfirm(){
	confirm_info("数据已更新");
}

/*
 * 对应字段名称格式校验（只能输入中文）
 */
function checkColumn(id){
	var regex = /^[^\u4e00-\u9fa5]{0,}$/;
	var name = $(id).val();
	if(!regex.test(name)){
		 confirm_info("对应字段名称不符合规范");
 	 }
}
function loadRespInfo(){
	
	var param = new Object();
	param.apiId = $("#apiId").val();
	
	/*
	 * param.docId = $("#docId").val();
	 */
	var apiId = $("#apiId").val();
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/list',
		data: param,
		success:
			  function(data){
		
					var tableBody = $("#responseTab tbody");
					
					loadDetail();
					
					// 先清空再加载新数据
					tableBody.empty();
					
					
					
					var row;
					var $row;
					var rowData;
					for(var i=0;i < data.responseList.length; i++){
						
						rowData = data.responseList[i];
						row = getTmpl("#respInfoTmpl")[0];
				
						$row = $(row);
						
						// 设置序号
						$row.find("td:first-child").html(i + 1);
						tableBody.append(row);
						
						$row.find("*").setFieldsValue(rowData);
						
						// 初始化更新操作
						$row.find(".oper-update").bind("click",rowData.id,function(e){
							updatePre(e.data);
						});
						
						// 初始化删除操作
						$row.find(".oper-del").bind("click",rowData.id,function(e){
							deleteResponse(e.data);
						});
					}
				}
	
	});
	
	
	
}


function serializeCustSchemaForm(){
	
	
	var param = $("#respCustSchemaForm").find("*").getFieldsValue();
	
	     param.valid = true;
		var nodeArray = new Array();
		$("#rootPoint").treegrid("getAllNodes").each(function(index,element){
			var $element = $(element);
	     
			var rowData = $element.find("*").getFieldsValue();
	       
			rowData.nodeId = $element.treegrid("getNodeId");
			
			rowData.parentId = $element.treegrid("getParentNodeId");
		    
		    
			nodeArray[index] = rowData;
			
	});
		
		param.childrenList = JSON.stringify(nodeArray);

		return param;
}
function serializeCustSchemaForm1(){
	
	
	var param = $("#respCustSchemaForm1").find("*").getFieldsValue();
	
	param.id = $("#responseId").val();
	
   
		var nodeArray = new Array();
		$("#rootPoint1").treegrid("getAllNodes").each(function(index,element){
			var $element = $(element);
			
			var rowData = $element.find("*").getFieldsValue();
	       
			rowData.nodeId = $element.treegrid("getNodeId");
			
			rowData.parentId = $element.treegrid("getParentNodeId");
		    rowData.topId =  $("#responseId").val();
		    rowData.id = 0;
			nodeArray[index] = rowData;
			
	});
		

		param.childrenList = JSON.stringify(nodeArray);

		return param;
}


/**
 * 通过选择器获取模板内容对应的jquery对象 <script id="reqParamTmpl" type="text/html">
 * <tr></tr>
 * </script>
 * 
 * @param selector
 * @returns
 */
function getTmpl(selector){
	return $($(selector).html());
}


/**
 * 初始化字段
 */
$.fn.setFieldsValue = function(data){
	this.each(function() {
		var $this = $(this);
		var name = this.name;
        var type = this.type;
        var tagName = this.tagName.toLowerCase();
        var value;
        if ((tagName == 'textarea') 
        		|| (tagName == 'input') 
        		|| (tagName == 'select')) {
        	value = data[name];
        	// alert(value);
        	if(value != undefined){
        		$this.val("" + value);
        	}
        }
        else if (type == 'checkbox' || type == 'radio') {

        }
        else if (type == "file") {

        }
    });
};

/**
 * 获取值
 */
$.fn.getFieldsValue = function(){
	var result = new Object();
	this.each(function() {
		var $this = $(this);
		var name = this.name;
        var type = this.type;
        var tagName = this.tagName.toLowerCase();
        var value;
        if ((tagName == 'textarea') 
        		|| (tagName == 'input') 
        		|| (tagName == 'select')) {
        	value = $this.val();
        	result[name] = value;
        }
        else if (type == 'checkbox' || type == 'radio') {

        }
        else if (type == "file") {

        }
    });
	
	return result;
};
/* 删除多余的行 */
function deleteRoot(id) {
	
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/deleteRoot',
		data: {id:id},
		success:function(data){
			if(data.code==200){
			
			}
		}
	});
}
/*点击是否分页触发时间*/
function checkboxOnclick(checkbox,apiId){
	if (checkbox.checked == true){
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: ctx + '/api/'+ apiId +'/request/addParam',
			data: {apiId:apiId},
			success:function(data){
				if(data.code==200){
					loadRequest();
				}
			}
		});
	}else{
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: ctx + '/api/'+ apiId +'/request/deleteParam',
			data: {apiId:apiId},
			success:function(data){
				if(data.code==200){
					loadRequest();
				}
			}
		});
	}
}

/*自定义sql*/
function sqlDesign(){
  var id = $("#sqlId").val();
  var sqlContext = $("#sql_textarea").val();
  var apiId = $("#apiId").val();
  $.ajax({
		type: 'POST',
		dataType: 'json',
		url: ctx + '/api/'+ apiId +'/response/sqlDesign',
		data: {id:id,sqlContext:sqlContext,apiId:apiId},
		success:function(data){
			if(data.code==200){
				loadRespInfo();
			}
		}
  });
}

